//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    uid: "",
    imgUrl: baseJs.imgUrl,
    // 
    indicatorDots: true,
    autoplay: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    indicatorActiveColor: "#fff",
    // 
    pageData: [],
    // 
    pageIndex: 1,
    state: "",
    courseList: [],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetCourseAdvertise();
    this.GetRecommandCourse();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.data.pageIndex++;
    this.GetRecommandCourse();
  },

  GetCourseAdvertise: function () {
    var that = this;
    var pageData = this.data.pageData;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetCourseAdvertise"),
      success: function (res) {
        pageData = JSON.parse(baseJs.decryUrl(res.data));
        console.log(pageData);
        if (pageData.ResultData != "") {
          pageData.ResultCount[0].url = "/packageA/class0/class0";
          pageData.ResultCount[1].url = "/packageA/class1/class1";
          pageData.ResultCount[2].url = "/packageA/class2/class2";
          pageData.ResultCount[3].url = "/packageA/class3/class3";
          that.setData({
            pageData: pageData
          })
        }
      }
    })
  },
  //
  GetRecommandCourse: function () {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var courseList = this.data.courseList;
    if (pageIndex == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetRecommandCourse&PageSize=10&PageIndex=" + pageIndex),
      success: function (res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          courseList.push(json[i])
        }
        that.setData({
          courseList: courseList,
        })
        // console.log(courseList);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
        wx.hideLoading();
      }
    })
  },
})